use rusqlite::{Connection, Result};
use crate::data::db_path;
use crate::domin::Repo;


#[tauri::command]
pub fn repo_change(id:String,count:String) -> usize {
    let sql = "update tb_repo set count = count - ? where id = ?;";
    let conn = Connection::open(db_path()).unwrap();
    let mut stmt = conn.prepare(sql).unwrap();
    stmt.execute([count,id]).unwrap()
}

pub fn repo_delete (id:String)-> Result<usize>{
    let conn = Connection::open(db_path())?;
    let mut stmt = conn.prepare("DELETE FROM tb_repo WHERE id = ?")?;
    let i = stmt.execute([id])?;
    Ok(i)
}

pub fn repo_insert (repo:Repo) -> Result<usize>{
    let conn = Connection::open(db_path())?;
    let mut stmt = conn.prepare("INSERT INTO tb_repo (id,count) VALUES (?,?)")?;
    let i = stmt.execute([repo.id,repo.count.to_string()])?;
    Ok(i)
}
